//
//  UUCollectionView.swift
//  quanyihui
//
//  Created by Galaxy on 2022/7/28.
//

import UIKit

class UUCollectionView: UICollectionView,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    @objc var listArr:[Any] = []{
        didSet{
            reloadData()
        }
    }
    
    @objc var scrollViewDidScrollBlock: ((_ scrollView: UIScrollView) -> ())?
    private var cellItemDidSelectedBlock:((_ indexPath:IndexPath)->())?
    private var cellForRowBlock:((_ indexPath:IndexPath)-> UICollectionViewCell)?
    private var sizeForItemBlock:((_ indexPath:IndexPath)-> CGSize)?
    ///设置点击事件
    @objc func cellItemDidSelected(_ closure:@escaping (_ indexPath:IndexPath)->Void){
        cellItemDidSelectedBlock = closure
    }
    ///设置cell样式
    @objc func cellForRow(_ closure:@escaping (_ indexPath:IndexPath)->UICollectionViewCell){
        cellForRowBlock = closure
    }
    
    @objc func sizeForItem(_ closure:@escaping (_ indexPath:IndexPath)->CGSize){
        sizeForItemBlock = closure
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        delegate = self
        dataSource = self
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        listArr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        cellForRowBlock!(indexPath)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        sizeForItemBlock!(indexPath)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if cellItemDidSelectedBlock != nil {
            cellItemDidSelectedBlock!(indexPath)
        }
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollViewDidScrollBlock != nil {
            scrollViewDidScrollBlock!(scrollView)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
